package com.zhongkai.encryptRSA;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

public class EncryptRSA {
	private Cipher mCipher = null;
	
	public EncryptRSA( ) throws NoSuchAlgorithmException, NoSuchPaddingException {
		Security.addProvider( new com.sun.crypto.provider.SunJCE( ) );
		mCipher = Cipher.getInstance( "RSA" );
	}
	
	public byte [] Encrypt (RSAPublicKey publicKey, byte[] src) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
		mCipher.init( Cipher.ENCRYPT_MODE, publicKey );
		return mCipher.doFinal( src );
	}
	
	public byte [] Decrypt(RSAPrivateKey privateKey, byte [] dst) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
		mCipher.init( Cipher.DECRYPT_MODE, privateKey );
		return mCipher.doFinal( dst );
	}
}
